﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EyFramework.Common;
using EyFramework.SocketServer.Behavior;
using EyFramework.SocketServer.Utils;
using WebSocketSharp.Server;

namespace EyFramework.SocketServer
{
    public class SchisSocetServer
    {
        private IniFileUtil iniFileUtil = new IniFileUtil(Path.Combine(DirectoryUtil.AssemblyDirectory(), "SocketConfig.ini"));
        private WebSocketServer wssv;

        public void start()
        {
            string port = iniFileUtil.IniReadValue("SocketServer", "SocketPort", "7000");
            wssv = new WebSocketServer(Int32.Parse(port));
            //医保服务
            wssv.AddWebSocketService<InsuranceBehavior>("/insurance");
            //市民卡服务
            wssv.AddWebSocketService<MisposBehavior>("/mispos");
            //远程控制服务
            wssv.AddWebSocketService<RemoteBehavior>("/remotectl");
            //系统管理相关服务
            wssv.AddWebSocketService<SysManagerBehavior>("/sysmanage");
            try
            {
                wssv.Start();
                Logger.Info("Socket 服务启动成功 端口:" + port);
            }
            catch (Exception e)
            {
                Logger.Info("Socket 服务启动失败 端口:" + port);
                MessageBox.Show("服务启动失败:" + e.Message, "服务器启动失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void stop()
        {
            if (wssv != null)
            {
                wssv.Stop();
                Logger.Info("Socket 服务已经停止");
            }
        }
    }
}